package com.itheima.CSM_bacK.aabServices;

import com.itheima.CSM_bacK.aabServices.Serv.ComServ;
import com.itheima.CSM_bacK.aacMapper.ComMapper;

import com.itheima.pojo.Entity.Setmeal;
import com.itheima.pojo.VO.*;
import org.apache.ibatis.javassist.LoaderClassPath;
import org.apache.poi.ss.formula.functions.Today;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class ComImpl implements ComServ {
    @Autowired
    private ComMapper comMapper;
//------------------------------------------------------------

    @Override
    public VOrGetMMs getMemberReport() {
        List<LocalDate> dDates = comMapper.getMonths();

        List<String> months = new ArrayList<>();
        List<Integer> countMembers = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.CHINA);
        for (LocalDate date : dDates) {
            String okok = LocalDate.of(date.getYear(),date.getMonth(),date.lengthOfMonth()).format(formatter);
            if (!months.contains(okok)){
                months.add(okok);
                LocalDate firstDay = LocalDate.of(date.getYear(),date.getMonth(),1);
                LocalDate lastDay = LocalDate.of(date.getYear(),date.getMonth(),date.lengthOfMonth());
                Integer kk = comMapper.getCounts(firstDay,lastDay);
                countMembers.add(kk);
            }
        }

        return new VOrGetMMs(months,countMembers);
    }

    @Override
    public VOrGetSR getSetmealReport() {
        List<String> names = new ArrayList<>();
        List<voTip> vvo = new ArrayList<>();
        names = comMapper.getNames();
        for (String name : names) {
            int kk = comMapper.getcountsByName(name);
            vvo.add(new voTip(name,kk));
        }
        return new VOrGetSR(names, vvo);
    }

    @Override
    public VOrGetBRD getBusinessReportData() {
        VOrGetBRD brd = new VOrGetBRD();
        LocalDate date = LocalDate.now();
        brd.setTodayNewMember(comMapper.getCounts(date,date));
        brd.setThisMonthNewMember(comMapper.getCounts(date.withDayOfMonth(1),date.withDayOfMonth(date.lengthOfMonth())));
        brd.setTodayVisitsNumber(comMapper.getCountsOrder(date,date));
        brd.setThisWeekNewMember(comMapper.getCounts(date.with(DayOfWeek.MONDAY),date));
        brd.setThisMonthOrderNumber(comMapper.getCountsOrder(date.withDayOfMonth(1),date.withDayOfMonth(date.lengthOfMonth())));
        brd.setTotalMember(comMapper.getCounts(LocalDate.MIN,date));
        brd.setReportDate(LocalDate.now());
        brd.setThisWeekOrderNumber(comMapper.getCountsOrder(date.with(DayOfWeek.MONDAY),date.with(DayOfWeek.SUNDAY)));
        brd.setTodayOrderNumber(comMapper.getCountsOrder(date,date));
        brd.setThisWeekVisitsNumber(comMapper.getCountsVisit(date.with(DayOfWeek.MONDAY),date.with(DayOfWeek.SUNDAY)));
        brd.setThisMonthVisitsNumber(comMapper.getCountsVisit(date.withDayOfMonth(1),date.withDayOfMonth(date.lengthOfMonth())));
        List<voTip2> ttips = new ArrayList<>();
        List<Setmeal> kks = comMapper.getSetmeals();
        int total = comMapper.getCountsOfAllSetmeal();
        for (Setmeal kk : kks) {
            voTip2 v = new voTip2();
            v.setName(kk.getName());
            v.setSetmeal_count(comMapper.getCountsOfSetmeal(kk.getName()));
            double proportion = Math.round(((float) v.getSetmeal_count() /total)*10000)/10000.0 ;
            v.setProportion(proportion);
            ttips.add(v);
        }
        brd.setHotSetmeal(ttips);

//        LocalDate reportDate;✅
//        int todayNewMember;✅
//        int thisWeekVisitsNumber;✅
//        int thisWeekNewMember;✅
//        int totalMember;✅
//        int thisMonthOrderNumber;✅
//        int thisMonthVisitsNumber;✅
//        int todayOrderNumber;✅
//        int thisWeekOrderNumber;✅
//        List<voTip2> hotSetmeal;

        return brd;
    }
}
